﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XProject.DataVisit.Model;
using System.Web;
using XProject.DataVisit.Factory;
using XProject.DataVisit.Model.Role;

namespace XProject.DataVisit.Handler.View.BaseManager
{
    public class RoleView:AbstractView
    {
        private UserInfo CurrentUser;
        public override void init()
        {
            if (HttpContext.Current.Session["UserInfo"] != null)
            {
                CurrentUser = HttpContext.Current.Session["UserInfo"] as UserInfo;
                base.init();
            }
            else
            {
                return;
            }
        }

        #region 角色管理
        public void GetRoles()
        {
            JsonResponse.ResponseObject = DaoFactory.RoleDao.GetAll();
        }

        public void AddRole()
        {
            var role = JsonRequest.GetParam<GYRole>("info");
            role.LastUpdate = DateTime.Now;
            role.Creater = CurrentUser.PID;
            JsonResponse.ResponseObject= DaoFactory.RoleDao.Save(role);
        }

        public void UpdateRole()
        {
            var role = JsonRequest.GetParam<GYRole>("info");
            JsonResponse.ResponseObject = DaoFactory.RoleDao.Update(role);
        }

        public void DelRole()
        {
            var role = JsonRequest.GetParam<GYRole>("info");
            JsonResponse.ResponseObject = DaoFactory.RoleDao.Delete(role);
        }
        #endregion

        #region 菜单管理
        public void GetMenus()
        {
            JsonResponse.ResponseObject = DaoFactory.MenuDao.GetParentList();
        }

        public void AddMenu()
        {
            var menu = JsonRequest.GetParam<GYMenu>("info");
            menu.SysImg = "../" + menu.MenuImg;
            JsonResponse.ResponseObject = DaoFactory.MenuDao.Save(menu);
        }

        public void UpdateMenu()
        {
            var menu = JsonRequest.GetParam<GYMenu>("info");
            menu.SysImg = "../" + menu.MenuImg;
            JsonResponse.ResponseObject = DaoFactory.MenuDao.Update(menu);
        }

        public void DelMenu()
        {
            var menu = JsonRequest.GetParam<GYMenu>("info");
            JsonResponse.ResponseObject = DaoFactory.MenuDao.Delete(menu);
        }
        #endregion

        #region 权限管理
        public void GetAccess()
        {
            var roleid = JsonRequest.GetParam<Guid>("id");
            JsonResponse.ResponseObject = DaoFactory.AccessDao.GetRoleList(roleid);
        }

        public void UpdateAccess()
        {
            var access = JsonRequest.GetParam<IList<GYAccess>>("info");
            var tempaccess = DaoFactory.AccessDao.GetRoleList(access[0].RoleID);
            foreach (var k in tempaccess)
            {
                DaoFactory.AccessDao.Delete(k);
            }
            for(int i=0;i<access.Count;i++)
            {
                access[i].Creater = CurrentUser.PID;
                access[i].LastUpdate = DateTime.Now;
                DaoFactory.AccessDao.Save(access[i]);
            }
            JsonResponse.HasError = false;
        }

        /// <summary>
        /// 获取当前登录用户权限
        /// </summary>
        public void GetMyAccess()
        { 
            
        }
        #endregion
    }
}
